package edu.ccut.saturn.workflow.finishTaskModule.code;

import java.util.HashMap;
import java.util.List;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.LogicKeyInfo;
import edu.ccut.saturn.workflow.model.ParameterInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;

public class ExecuteCustomLogic extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		HashMap <String ,String > formdatavalue = (HashMap<String,String>)parameterContext.getParameterValue(0);
		WorkFlowInfo workFlowInfo = (WorkFlowInfo)parameterContext.getParameterValue(1);
		String activity_define_id = (String ) parameterContext.getParameterValue(2);
		// 获得当前工作项对应活动的定义信息
		ActivityInfo activity = workFlowInfo.getActivityInfoById(activity_define_id);
		// 如果有自定义logic需要执行，就先执行之

		List<LogicKeyInfo> logicList = activity.getLogicKey();
		if (logicList != null && logicList.size() > 0) {
			for (LogicKeyInfo logic : logicList) {
				IParameterContext tempParameterContext = ComponentFactory// 上下文不能重用
						.createParameterContext();
				// action =
				// ComponentManager.getInstance().getComponent(logic.getFullKey());
				debug("sublogic [" + logic.getFullKey() + "] has been started",parameterContext); //$NON-NLS-1$ //$NON-NLS-2$
				// 设置ｌｏｇｉｃ需要的上下文参数
				List<ParameterInfo> paramList = logic.getParamList();
				for (ParameterInfo paramInfo : paramList) {
					String value = (String) formdatavalue.get(paramInfo.getKey());//formdatavalue已经是得到最新值的
					tempParameterContext.addParameter(value, String.class);
				}
				// action.execute(tempParameterContext);
				executeLogicSupportsTransaction(parameterContext,
						tempParameterContext, logic.getFullKey());
			}
		}
		
	}
}